﻿Imports System.Drawing
Imports System.Threading
Imports System.IO
Public Class MainForm
    Private curr_color As Color = Color.White
    Private hex_color() As String = {"00", "00", "00"}
    Private rgb_color() As Byte = {"0", "0", "0"}
    Public palettes As New ArrayList
    Public user As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "/Bluegrams/MINI Office"

#Region "MainForm Methods"

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim language As String
        statInfo.Text = ""
        statAbout.Text = "v." & Me.ProductVersion & " by Bluegrams 2016"
        language = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName
        If language = "de" Then
            conLanguageEng.Checked = False
            conLanguageDe.Checked = True
        Else
            conLanguageEng.Checked = True
            conLanguageDe.Checked = False
        End If
        If Directory.Exists(user) = False Then
            Directory.CreateDirectory(user)
            File.Create(user & "/hashFF_pal.blu").Dispose()
            Directory.CreateDirectory(user & "/Palettes")
        Else
            check_Palettes()
        End If
        Me.KeyPreview = True
    End Sub

    Private Sub check_Palettes()
        Dim regr As New StreamReader(user & "/hashFF_pal.blu")
        Do Until regr.EndOfStream
            palettes.Add(regr.ReadLine())
        Loop
        regr.Close()
        If palettes.Count > 0 Then
            butRename.Enabled = True
            butDelete.Enabled = True
            panPal.Enabled = True
            ComPalettes_DropDown(Me, New EventArgs)
            comPalettes.SelectedIndex = 0
            palette_change()
        End If
    End Sub

    Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        txtR.Focus()
        If My.Application.CommandLineArgs.Count > 0 Then
            Dim starting_arg As String = My.Application.CommandLineArgs(0)
            If starting_arg = "/detect" Then
                butDetect_Click(sender, e)
            End If
        End If
    End Sub

    Private Sub MainForm_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.F1 Then
            conAbout_Click(sender, e)
        End If
    End Sub

    Private Sub keycheck(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
        If e.KeyCode = Keys.Return AndAlso timDetect.Enabled Then
            butDetect_Click(sender, e)
        End If
    End Sub

    Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        Dim regw As New StreamWriter(user & "/hashFF_pal.blu")
        For Each item In palettes
            regw.WriteLine(item.ToString)
        Next
        regw.Close()
    End Sub

#End Region

#Region "Convert Colours"

    Private Sub firstcol_TextChanged(sender As Object, e As EventArgs) Handles txtR.TextChanged, txtH.TextChanged
        If sender Is txtR Then
            toHEX(0, txtR.Text, sender, txtH)
        Else
            toRGB(0, txtH.Text, sender, txtR)
        End If
    End Sub

    Private Sub seccol_TextChanged(sender As Object, e As EventArgs) Handles txtG.TextChanged, txtE.TextChanged
        If sender Is txtG Then
            toHEX(1, txtG.Text, sender, txtE)
        Else
            toRGB(1, txtE.Text, sender, txtG)
        End If
    End Sub

    Private Sub thirdcol_TextChanged(sender As Object, e As EventArgs) Handles txtB.TextChanged, txtX.TextChanged
        If sender Is txtB Then
            toHEX(2, txtB.Text, sender, txtX)
        Else
            toRGB(2, txtX.Text, sender, txtB)
        End If
    End Sub

    Private Sub toHEX(pos As Integer, value As String, ByRef box As TextBox, ByRef cor As TextBox)
        If value = "" Then value = "0"
        Try
            rgb_color(pos) = value
            hex_color(pos) = Hex(value)
            curr_color = Color.FromArgb(rgb_color(0), rgb_color(1), rgb_color(2))
            panData.BackColor = curr_color
            If box.Text = "" Then
                cor.Text = ""
            Else
                cor.Text = hex_color(pos)
            End If
        Catch
            box.Text = rgb_color(pos)
            cor.Text = hex_color(pos)
        End Try
    End Sub

    Private Sub toRGB(pos As Integer, value As String, ByRef box As TextBox, ByRef cor As TextBox)
        If value = "" Then value = "0"
        Try
            rgb_color(pos) = Convert.ToInt16(value, 16)
            hex_color(pos) = value
            curr_color = Color.FromArgb(rgb_color(0), rgb_color(1), rgb_color(2))
            panData.BackColor = curr_color
            If box.Text = "" Then
                cor.Text = ""
            Else
                cor.Text = rgb_color(pos)
            End If
        Catch
            box.Text = rgb_color(pos)
            cor.Text = hex_color(pos)
        End Try
    End Sub

    Private Sub from_Panel()
        rgb_color(0) = curr_color.R
        rgb_color(1) = curr_color.G
        rgb_color(2) = curr_color.B
        txtR.Text = curr_color.R
        txtG.Text = curr_color.G
        txtB.Text = curr_color.B
        toHEX(0, curr_color.R, txtR, txtH)
        toHEX(1, curr_color.G, txtG, txtE)
        toHEX(2, curr_color.B, txtB, txtX)
    End Sub

#End Region

#Region "Edit Palettes"

    Private Sub contxtPalette_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles contxtPalette.Opening
        If butRename.Enabled Then
            conRename.Enabled = True
        Else
            conRename.Enabled = False
        End If
        If butDelete.Enabled Then
            conDelete.Enabled = True
        Else
            conDelete.Enabled = False
        End If
    End Sub

    Private Sub butNew_Click(sender As Object, e As EventArgs) Handles butNew.Click, conNew.Click
        Dim Palette_Edit As New Palette_Edit(My.Resources.pal_edit_new, -1)
        Palette_Edit.ShowDialog()
        txtR.Focus()
    End Sub

    Private Sub butRename_Click(sender As Object, e As EventArgs) Handles conRename.Click, butRename.Click
        Dim Palette_Edit As New Palette_Edit(My.Resources.pal_edit_rename, comPalettes.SelectedIndex)
        Palette_Edit.ShowDialog()
        ComPalettes_DropDown(sender, e)
        txtR.Focus()
    End Sub

    Private Sub butDelete_Click(sender As Object, e As EventArgs) Handles butDelete.Click, conDelete.Click
        If MessageBox.Show(My.Resources.pal_delete_text, My.Resources.pal_delete_title, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) =
            MsgBoxResult.Yes Then
            File.Delete(user & "/Palettes/" & palettes(comPalettes.SelectedIndex).ToString & ".blu")
            If palettes.Count > 1 Then
                palettes.RemoveAt(comPalettes.SelectedIndex)
                If comPalettes.SelectedIndex <> 0 Then comPalettes.SelectedIndex -= 1
                ComPalettes_DropDown(sender, e)
                palette_change()
            ElseIf palettes.Count = 1 Then
                palettes.Clear()
                clear_Palette()
                comPalettes.Items.Clear()
                butRename.Enabled = False
                butDelete.Enabled = False
                panPal.Enabled = False
            End If
        End If
        txtR.Focus()
    End Sub

    Friend Sub clear_Palette()
        For Each but As Button In panPal.Controls
            but.BackColor = Color.White
            but.FlatAppearance.BorderSize = 1
            but.FlatAppearance.MouseOverBackColor = Color.White
            but.FlatAppearance.MouseDownBackColor = Color.White
        Next
    End Sub

    Private Sub palette_change()
        clear_Palette()
        Dim nr As New StreamReader(user & "/Palettes/" & comPalettes.SelectedItem.ToString & ".blu")
        For Each but As Button In panPal.Controls
            Try
                Dim string_col() As String = nr.ReadLine.Split(";")
                Dim saved_col() As Integer = {0, 0, 0}
                For i = 0 To 2
                    saved_col(i) = Convert.ToInt16(string_col(i))
                Next
                but.BackColor = Color.FromArgb(saved_col(0), saved_col(1), saved_col(2))
                but.FlatAppearance.MouseDownBackColor = Color.FromArgb(saved_col(0), saved_col(1), saved_col(2))
                but.FlatAppearance.MouseOverBackColor = Color.FromArgb(saved_col(0), saved_col(1), saved_col(2))
            Catch
                but.BackColor = Color.White
            End Try
        Next
        nr.Close()
    End Sub

#End Region

#Region "Editmenu"

    Private Sub butAdd_Click(sender As Object, e As EventArgs) Handles butAdd.Click, conAdd.Click
        Dim place As Button = Nothing
        For Each but As Button In panPal.Controls
            If but.FlatAppearance.BorderSize = 2 Then
                place = but
            End If
        Next
        If place IsNot Nothing Then
            place.BackColor = curr_color
            place.FlatAppearance.MouseOverBackColor = curr_color
            place.FlatAppearance.MouseDownBackColor = curr_color
            Dim sw As New StreamWriter(user & "/Palettes/" & palettes(comPalettes.SelectedIndex).ToString & ".blu")
            For Each but As Button In panPal.Controls
                If but.BackColor = Color.White Then
                    sw.WriteLine("/")
                Else
                    sw.WriteLine(but.BackColor.R & ";" & but.BackColor.G & ";" & but.BackColor.B)
                End If
            Next but
            sw.Close()
        End If
        txtR.Focus()
    End Sub

    Private Sub butCopy_Click(sender As Object, e As EventArgs) Handles butCopy.Click, conCopy.Click
        If txtH.Text <> "" Or txtE.Text <> "" Or txtX.Text <> "" Then
            Dim to_clip As String = "#"
            For Each part In hex_color
                If part.Length = 1 Then part = "0" & part
                to_clip &= part
            Next
            Clipboard.SetText(to_clip)
        End If
        txtR.Focus()
    End Sub

    Private Sub contxtEdit_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles contxtEdit.Opening
        If txtH.Text <> "" Or txtE.Text <> "" Or txtX.Text <> "" Then
            conCopy.Enabled = True
        Else
            conCopy.Enabled = False
        End If
    End Sub

#End Region

#Region "Palette Controls"

    Friend Sub ComPalettes_DropDown(sender As Object, e As EventArgs) Handles comPalettes.DropDown
        Dim selected As Integer = comPalettes.SelectedIndex
        comPalettes.Items.Clear()
        For Each pal In palettes
            comPalettes.Items.Add(pal)
        Next
        comPalettes.SelectedIndex = selected
    End Sub

    Private Sub Palette_Click(sender As Button, e As EventArgs) Handles but1.Click, but2.Click, but3.Click, but4.Click, but5.Click, but6.Click,
        but7.Click, but8.Click, but9.Click, but10.Click, but11.Click, but12.Click
        For Each but As Button In panPal.Controls
            but.FlatAppearance.BorderSize = 1
        Next
        sender.FlatAppearance.BorderSize = 2
        If sender.BackColor <> Color.White Then
            curr_color = sender.BackColor
            from_Panel()
        End If
    End Sub

    Private Sub comPalettes_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles comPalettes.SelectionChangeCommitted
        palette_change()
    End Sub

#End Region

#Region "Get Colours"

    Private Sub butDetect_Click(sender As Object, e As EventArgs) Handles butDetect.Click, conDetect.Click
        timDetect.Enabled = Not timDetect.Enabled
        If timDetect.Enabled Then
            statInfo.Text = My.Resources.detect
            statAbout.Text = My.Resources.detect_info
        Else
            statInfo.Text = ""
            statAbout.Text = "v." & Me.ProductVersion & " by Bluegrams 2016"
            txtR.Focus()
            txtR.SelectionLength = 0
        End If
    End Sub

    Private Sub detect_Color(sender As Object, e As EventArgs) Handles timDetect.Tick
        Dim map As New Bitmap(1, 1)
        Dim g As Graphics = Graphics.FromImage(map)
        g.CopyFromScreen(MousePosition.X, MousePosition.Y, 0, 0, New Size(100, 100))
        curr_color = map.GetPixel(0, 0)
        from_Panel()
    End Sub

    Private Sub butDialog_Click(sender As Object, e As EventArgs) Handles butDialog.Click, conDialog.Click
        If colDialog.ShowDialog() = DialogResult.OK Then
            curr_color = colDialog.Color
            panData.BackColor = curr_color
            from_Panel()
        End If
        txtR.Focus()
        txtR.SelectionLength = 0
    End Sub

#End Region

#Region "Set Language/ Options"

    Private Sub conLanguageEng_Click(sender As Object, e As EventArgs) Handles conLanguageEng.Click
        Dim new_culture As New Globalization.CultureInfo("en")
        change_Culture(new_culture, sender, e)
    End Sub

    Private Sub conLanguageDe_Click(sender As Object, e As EventArgs) Handles conLanguageDe.Click
        Dim new_culture As New Globalization.CultureInfo("de")
        change_Culture(new_culture, sender, e)
    End Sub

    Private Sub change_Culture(culture As Globalization.CultureInfo, sender As Object, e As EventArgs)
        Thread.CurrentThread.CurrentUICulture = culture
        Thread.CurrentThread.CurrentCulture = culture
        Me.Controls.Clear()
        palettes.Clear()
        Me.DataBindings.Clear()
        InitializeComponent()
        txtR.Focus()
        MainForm_Load(sender, e)
        If curr_color <> Color.White Then
            panData.BackColor = curr_color
            from_Panel()
        End If
    End Sub

    Private Sub conAbout_Click(sender As Object, e As EventArgs) Handles conAbout.Click
        Info_Window.ShowDialog()
    End Sub

#End Region

End Class